package com.itbaizhan.rocketmqproducer.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.List;

/*消息的查询*/
public class QueryingMessageDemo {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //创建消费者对象
        DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer("producer_grp_01");
        //设置nameserv地址
        pushConsumer.setNamesrvAddr("192.168.66.100:9876");
        //设置消费监听器
        pushConsumer.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        pushConsumer.start();
        //根据messageid查询消息
        MessageExt message = pushConsumer.viewMessage("topic_springboot_01","C0A8006727F418B4AAC231E22BAC0000");
        System.out.println(message);
        System.out.println(message.getMsgId());
        pushConsumer.shutdown();
    }
}
